//引入pinia
import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { permission } from '@/api'
import { menu } from '@/router/menu.js'
import router from '@/router/index.js'
import { rulesMenu } from '@/utils/common.js'
import { cloneDeep } from 'lodash'
//通过 defineStore 创建的一个 store
export const usemenuInfoStore = defineStore('menuInfo', () => {
  //一个异步函数，用于执行获取 API 请求
  const menuInfo = reactive([])
  const changemenuInfo = async () => {
    const result = await permission()
    let rightmenu = rulesMenu(menu, result.data)
    // console.log(rightmenu);
    menuInfo.push(...rightmenu)
    // console.log(router.options.routes[0].children);
    //使用深拷贝是为了不影响原数据
    let arr = cloneDeep(router.options.routes)
    arr[0].children.push(...rightmenu)
    // console.log(arr);
    return rightmenu
  }
  const deletemenuInfo = () => {
    menuInfo.splice(0,menuInfo.length)
  }
  return { changemenuInfo, menuInfo, deletemenuInfo }
});